package com.wangp.mywrite.coding;

import java.util.Arrays;

/***
 * @author wangpeng
 * @since 2022-02-24  10:54
 */
public class FindBall {

    public int[] findBall(int[][] grid) {
        int m = grid[0].length;

        int[] ans = new int[m];
        for (int i = 0; i < m; i++) {
            int col = i;
            for (int[] rows : grid) {
                int move = rows[col];
                col += move;
                if (col < 0 || col >= m || rows[col] != move) {
                    ans[i] = -1;
                    break;
                }
            }
            if (ans[i] == 0) {
                ans[i] = col;
            }
        }

        return ans;
    }


    public static void main(String[] args) {
        int[][] grid = new int[][]{{1,1,1,1,1,1},{-1,-1,-1,-1,-1,-1},{1,1,1,1,1,1},{-1,-1,-1,-1,-1,-1}};

        FindBall findBall = new FindBall();
        int[] ball = findBall.findBall(grid);
        System.out.println(Arrays.toString(ball));
    }
}
